import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [

    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home/index.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/login.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/register/register.vue'),
    },
  ],
})

let listt = ['/login', '/register'];

router.beforeEach((to, from, next) => {

  let accessToken = localStorage.getItem('accessToken')
  if (!accessToken) {
    if (listt.includes(to.path)) {
      next('/login')
    }
    else {
      next()
    }
  }
  else {
    next();
  }
})

export default router
